#include <jni.h>
#include <string>
#include <android/log.h>
#include "control/GndControl.h"
#include "callback/CallBack.h"
#include "register/Register.h"

JavaVM *g_JavaVM;
extern "C"
JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *res) {
    JNIEnv *env;
    g_JavaVM = vm;
    if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
        return -1;
    }
//    av_jni_set_java_vm(vm,0);
    Register::getInstance()->register_classes(env);
    return JNI_VERSION_1_4;
}

extern "C" JNIEXPORT jstring JNICALL
Java_com_rykj_socket_1lib_SocketLib_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

extern "C"
JNIEXPORT void JNICALL
Java_com_rykj_socket_1lib_SocketLib_setCallBack(JNIEnv *env, jobject thiz, jobject call_back) {
    CallBack::getInstance()->init(env, call_back, g_JavaVM);
}

extern "C"
JNIEXPORT void JNICALL
Java_com_rykj_socket_1lib_SocketLib_Start(JNIEnv *env, jobject thiz, jstring ip, jint port) {
    const char *ip_c = env->GetStringUTFChars(ip, 0);
    GND_LOGD("ip ----> %s port ----> %d", ip_c, port)
    GndControl::getInstance()->Start(ip_c, port);

}

extern "C"
JNIEXPORT void JNICALL
Java_com_rykj_socket_1lib_SocketLib_Stop(JNIEnv *env, jobject thiz) {
    GndControl::getInstance()->Stop();
}

extern "C"
JNIEXPORT void JNICALL
Java_com_rykj_socket_1lib_SocketLib_QuerySetDev(JNIEnv *env, jobject thiz) {
    GndControl::getInstance()->QuerySetDev();
}

extern "C"
JNIEXPORT void JNICALL
Java_com_rykj_socket_1lib_SocketLib_QueryAlgParams(JNIEnv *env, jobject thiz, jchar dev_no) {
    GndControl::getInstance()->QueryAlgParams(dev_no);
}

extern "C"
JNIEXPORT void JNICALL
Java_com_rykj_socket_1lib_SocketLib_QueryTrackPtzPid(JNIEnv *env, jobject thiz) {
    GndControl::getInstance()->QueryTrackPtzPid();
}

extern "C"
JNIEXPORT void JNICALL
Java_com_rykj_socket_1lib_SocketLib_PtzMove(JNIEnv *env, jobject thiz, jint type, jint direction,
                                            jshort angle, jint speed) {
    GndControl::getInstance()->PtzMove(GndToSkyPtzTypeEnum(type), direction, angle, speed);
}

extern "C"
JNIEXPORT void JNICALL
Java_com_rykj_socket_1lib_SocketLib_ZoomAction(JNIEnv *env, jobject thiz, jchar dev_no,
                                               jint direction) {
    switch (direction) {
        case 0:
            GndControl::getInstance()->ZoomAction(dev_no, ZOOM_ENLARGE);
            break;
        case 1:
            GndControl::getInstance()->ZoomAction(dev_no, ZOOM_NARROW);
            break;
    }

}

extern "C"
JNIEXPORT void JNICALL
Java_com_rykj_socket_1lib_SocketLib_TrackAction(JNIEnv *env, jobject thiz, jchar dev_no,
                                                jobject box) {
    BoundingBox boundingBox;
    boundingBox.classId = env->GetCharField(box, Register::getInstance()->getBoxBlock().classId);
    boundingBox.xmin = env->GetShortField(box, Register::getInstance()->getBoxBlock().xmin);
    boundingBox.xmax = env->GetShortField(box, Register::getInstance()->getBoxBlock().xmax);
    boundingBox.ymax = env->GetShortField(box, Register::getInstance()->getBoxBlock().ymax);
    boundingBox.ymin = env->GetShortField(box, Register::getInstance()->getBoxBlock().ymin);
    GndControl::getInstance()->TrackAction(dev_no, boundingBox);
}

extern "C"
JNIEXPORT void JNICALL
Java_com_rykj_socket_1lib_SocketLib_SetPtzPID(JNIEnv *env, jobject thiz, jobject pid_info,
                                              jchar type) {
}

extern "C"
JNIEXPORT void JNICALL
Java_com_rykj_socket_1lib_SocketLib_SetDetectParams(JNIEnv *env, jobject thiz, jobject params,
                                                    jchar dev_no) {
}

extern "C"
JNIEXPORT void JNICALL
Java_com_rykj_socket_1lib_SocketLib_SetTrackParams(JNIEnv *env, jobject thiz, jobject track_params,
                                                   jchar dev_no) {
}